//$(document).ready(function () {
//	
//});

//当dom元素加载完成(不需要等待资源加载完成)
$(function () {
	//加载图片
	//	var img = document.createElement("img");
	//	console.log(img);
	
	//图片名数组
	var loadingImgs = ["img/activity_rule.png", "img/bg.jpg", "img/bg2.png", "img/close.png", "img/p1_btns_wrap.png", "img/p1_first.png", "img/p1_from.png", "img/p1_second.png", "img/p1_sub.png", "img/p1_third.png", "img/p2_kuang.png", "img/p2_qian.jpg", "img/p2_scoring.png", "img/p2_shou.png", "img/p2_txt1.png", "img/p2_txt2.png", "img/p2_txt3.png", "img/p2_zhuan.png", "img/p3_acquire.png", "img/p3_again.png", "img/p3_bg.jpg", "img/p3_share.png", "img/p3_share_btn.png", "img/prize.png", "img/qian.png", "img/ranking.png", "img/ranking_bg.png", "img/shiyong.png", "img/shizhong.png", "img/shou.png", "img/start_game.png", "img/tiaozhan.png", "img/yingqu.png"];
	//记录完成的数量
	var loadingCount = 0;
	//遍历数组, 加载图片
	for (var i = 0; i < loadingImgs.length; i++) {
		var img = new Image();
		img.src = loadingImgs[i];
		img.onload = function () {
			loadingCount++;
			//修改百分百
			var percent = parseInt(loadingCount / loadingImgs.length * 100);
			$("#loading").html(percent + "%");
			//判断加载是否完成
			if (loadingCount == loadingImgs.length) {
				console.log("加载完成");
				//隐藏加载页面
				$("#loading-wrap").hide();
				//显示第一页
				$("#page1").show();
			}
		}
	}
	
	//点击开始游戏
	$("#p1-start").click(function () {
		$("#info-alert").show();
	});
	
	//点击关闭按钮
	$(".close-button").click(function () {
		$(".alert-wrap").hide();
	});
	
	//点击提交
	$("form>input[type=button]").click(function () {
		console.log("提交资料");
		$(".alert-wrap").hide();
		$("#page1").hide();
		$("#page2").show();
		startGame();
	});
	
	//点击数钱榜
	$(".rank-button").click(function () {
		$.ajax({
			type:"get",
			url:"http://1.fuxiaohui926.applinzi.com/count-money/rank.php",
			dataType: "json",
			success: function (arr) {
				for (var i = 0; i < arr.length; i++) {
					//获取数据
					var data = arr[i];
					//展示数据
					$(".rank-photo").eq(i).find("img").attr("src", data.headimgurl);
					$(".rank-name").eq(i).html(data.nickname);
					$(".rank-score").eq(i).html(data.score + "分");
				}
			}
		});
		
		$("#rank-alert").show();
	});
	
	//点击活动规则
	$(".rule-button").click(function () {
		$("#rule-alert").show();
	});
	
	//点击活动奖品
	$(".prize-button").click(function () {
		$("#prize-alert").show();
	});
	
	//点击奖券使用说明
	$(".explain-button").click(function () {
		$("#explain-alert").show();
	});
	
	//阻止默认事件
	touch.on("#page2", "touchstart", function (e) {
		e.preventDefault();
	});
	
	//滑动手势
	var timer = null;
	var textTimer = null;
	var time = 5;
	var moneyCount = 0;//记录数量
	touch.on("#p2-money", "swipeup", function () {
		console.log("向上滑");
		//开始计时
		if (timer == null) {
			timer = setInterval(function () {
				time--;
				$(".time-clock").html(time);
				if (time == 0) {
					//清除计时器
					clearInterval(timer);
					timer = null;
					clearInterval(textTimer);
					//切换页面
					$("#page2").hide();
					$("#page3").show();
					//修改页面3的钱数
					$("#p3-money").html("￥" + moneyCount * 100);
					//请求php页面, 并传递数据
					$.ajax({
						type: "get",
						url: "http://1.fuxiaohui926.applinzi.com/count-money/save-data.php",
						data: {
							score: moneyCount,
							nickname: $("#nickname").val(),
							openid: $("#openid").val(),
							headimgurl: $("#headimgurl").val()
						},
						dataType: "json",
						success: function (data) {
							$("#p3-best").html("我的辉煌战绩:￥" + (data.bestScore * 100) + " 当前排名:" + data.bestRank + "位");
						}
					});
				}
			}, 1000);
		}
		//生成钱
		var money = $('<img class="fly-money" src="img/p2_qian.jpg">');
		$("#page2").append(money);
		setTimeout(function () {
			money.remove();
		}, 1000);
		//计数
		moneyCount++;
		//个位
		var num1 = moneyCount % 10;
		//十位
		var num2 = parseInt(moneyCount % 100 / 10);
		//百位
		var num3 = parseInt(moneyCount / 100);
		$(".time-number").eq(0).html(num3);
		$(".time-number").eq(1).html(num2);
		$(".time-number").eq(2).html(num1);
	});
	
	//开始游戏
	function startGame () {
		//重置数据
		moneyCount = 0;
		$(".time-number").html("0");
		time = 5;
		$(".time-clock").html("60");
		//文字切换
		var num = 1;
		textTimer = setInterval(function () {
			num++;
			if (num == 4) {
				num = 1;
			}
			$("#p2-text").attr("src", "img/p2_txt" + num + ".png");
		}, 2000);
	}
	
	//点击再来一次
	$("#p3-again").click(function () {
		//切换页面
		$("#page3").hide();
		$("#page2").show();
		startGame();
	});
	
	//点击分享
	$("#p3-share").click(function () {
		$("#share-alert").show();
	});
	
	//取消分享
	$("#share-alert").click(function () {
		$(this).hide();
	});
	
});
